package org.noote.libs.sogle;

import android.util.FloatMath;

public class Camera {

	public Vector3D _position;
	public Vector3D _front, _left, _up; // Normalized vector

	Camera()
	{
		_position = new Vector3D(0, 0, 0);
		_front = new Vector3D(0, 0, 1);
		_up = new Vector3D(0, 1, 0);
		_left = new Vector3D(1, 0, 0);		
	}
	public Camera(float px, float py, float pz, float wx, float wy, float wz)
	{
		_position = new Vector3D(px, py, pz);
		_front = new Vector3D(wx, wy, wz);
		_up = new Vector3D(wy, wz, wx);
		_left = new Vector3D(wz, wx, wy);		
	}
	public Camera(Vector3D p, Vector3D lookAt, float roll)
	{
		setLookAt(p, lookAt, roll);
	}
	
	public void setLookAt(Vector3D p, Vector3D lookAt, float roll)
	{
		_position = p;
		_front = Vector3D.sub(lookAt, p);
		_front.normalize();
		
		Vector3D _roll = new Vector3D(FloatMath.sin(roll), FloatMath.cos(roll), 0);
		_left = Vector3D.crossProduct(_roll, _front);
		_left.normalize();
		
		_up = Vector3D.crossProduct(_front, _left);
		_up.normalize();
	}

	public void move_forward(float speed)
	{
		_position.x += _front.x*speed;
		_position.y += _front.y*speed;
		_position.z += _front.z*speed;
	}
	
	public void move_left(float speed)
	{
		_position.x += _left.x*speed;
		_position.y += _left.y*speed;
		_position.z += _left.z*speed;
	}

	public void move_up(float speed)
	{
		_position.x += _up.x*speed;
		_position.y += _up.y*speed;
		_position.z += _up.z*speed;
	}

	public void rotate_view_left(float speed)
	{
		float vx=(float)Math.cos(speed)*_front.x-(float)Math.sin(speed)*_front.z;
		float vz=(float)Math.sin(speed)*_front.x+(float)Math.cos(speed)*_front.z;
		
		_front.x = vx;
		_front.z = vz;

		_left = Vector3D.crossProduct(_up, _front);
		_left.normalize();

		_up = Vector3D.crossProduct(_front, _left);
		_up.normalize();
	}
}
